package com.naza.rpc.discovery;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.naza.rpc.client.ConsClient;
import com.naza.rpc.client.NacosClient;
import com.naza.rpc.model.ServiceInstance;
import com.naza.rpc.util.InstanceUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yl
 */
public class ConsulDiscovery implements Discovery {

    ConsClient client = ConsClient.getINSTANCE();

    @Override
    public List<ServiceInstance> getAll(String serviceName) {
        List<ServiceInstance> serviceInstances = new ArrayList<>();
        try {
            List<Instance> instances = client.getAllInstance(serviceName);
            for(Instance instance : instances){
                serviceInstances.add(InstanceUtil.transfer(instance));
            }
        }catch (Exception e){

        }
        return serviceInstances;
    }

    @Override
    public void monitor(String serviceName,DiscoveryListener listener) {
        try {
            client.monitor(serviceName,listener);
        }catch (Exception e){

        }
    }
}
